cc.Class({
    extends: cc.Component,

    properties: {
        label: {
            default: null,
            type: cc.Label
        },
        dayPre:cc.Prefab,
        friPre:cc.Prefab,
        msgPre:cc.Prefab,

        // defaults, set visually when attaching this script to the Canvas
        text: 'Hello, World!'
    },

    // use this for initialization
    onLoad: function () {
        this.label.string = this.text;
        // console.log(aaa);
        this.myInfoPage = this.node.getChildByName("myInfoPage");
        this.qiandaoPage = this.node.getChildByName("qiandaoPage");
        this.qiandaoInfo = this.node.getChildByName("qiandaoInfo"); // 签到详情页面
        this.myInfoPage.getChildByName("body").getChildByName("logout").on("touchstart",function(){
            cc.sys.localStorage.removeItem('uid');
            cc.director.loadScene('login');
        });

        var date = new Date();  
        this.year = date.getFullYear();
        this.month = date.getMonth() + 1;
        this.changeNumber = 0; // 变化次数


        this.initUserInfo();
        // this.initSignInfo();
        this.initSignType();

        if (cc.sys.os == cc.sys.OS_ANDROID) {                    //安卓返回提示并退出
            cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, androidExit, this);
        }
    },
    update:function(dt){

    },
    // called every frame
    initSignType: function () {
        var self = this;
        jqueryHttp(url + "getTypeInfo",function(obj,error){
            console.log(obj);
            if(isnullOrundefined(obj)){
               return; 
            }
            var typecontent = self.qiandaoPage.getChildByName("type").getChildByName("view").getChildByName("content");
            typecontent.removeAllChildren();
            // 初始化 所有 类型
            var type2name = new Array();
            for (var i = 0; i < obj.length; i++) {
                type2name[obj[i].id] = obj[i].name;
                // obj[i]
                var node = new cc.Node('label');
                var label = node.addComponent(cc.Label);
                label.string = obj[i].name;
                label.fontSize = 25;
                node.typename = obj[i].name;
                node.typeID = obj[i].id;


                node.on("touchstart",function(){
                    self.showTypeContent();

                    console.log("this.typeID:" + this.typeID);
                    self.qiandaoPage.getChildByName("typeok").typeID = this.typeID;
                    self.qiandaoPage.getChildByName("typeok").getChildByName("Label").getComponent(cc.Label).string = this.typename;
                });

                node.parent = typecontent;
            }
        });
    },

    // 1 打开签到页面 2 打开个人页面
    showPage:function(event,pageId){
        this.qiandaoInfo.active = false;
        if(pageId == 1){

            this.myInfoPage.getChildByName("qiandaoPage").friend_uid = null;
            this.qiandaoPage.active = true;
            this.myInfoPage.active = false;

            this.initSignInfo();
        }

        if(pageId == 2){
            this.qiandaoPage.active = false;
            this.myInfoPage.active = true;
        }
    },

    changeFriendList:function(){
        console.log("changeFriendList");

        var friendList = this.myInfoPage.getChildByName("body").getChildByName("friend").getChildByName("friendList");
        if(friendList.active == false){
            friendList.active = true;
        }else{
            friendList.active = false;
        }
    },
    checkUserInfo:function(){
        var add = this.myInfoPage.getChildByName("body").getChildByName("add");
        var EditBoxName = add.getChildByName("EditBoxName").getComponent(cc.EditBox).string;
        if(EditBoxName==""){
            console.log("kong");
            return;
        }
        var self = this;
        jqueryHttp(url + "getUserInfoByAccount?account="+EditBoxName,function(obj,error){
            console.log(obj);
            var checkAccount = self.myInfoPage.getChildByName("body").getChildByName("checkAccount");
            checkAccount.active = true;
            if(isnullOrundefined(obj)){
                checkAccount.getChildByName("checkAccount").getComponent(cc.Label).string = "查无此人";
                checkAccount.u_id = null;
                return;
            }
            checkAccount.getChildByName("checkAccount").getComponent(cc.Label).string = obj.name;
            checkAccount.u_id = obj.id;
            checkAccount.getChildByName("add").on("touchstart",function(){
                this.addFriend();
            }.bind(self));

        });
    },
    addFriend:function(){
        var checkAccount = this.myInfoPage.getChildByName("body").getChildByName("checkAccount");
        if(isnullOrundefined(checkAccount.u_id)){
           return; 
        }

        var u_id = cc.sys.localStorage.getItem("uid");
        var u_id2 = checkAccount.u_id;
        var self = this;
        jqueryHttp(url + "addFriById?u_id="+u_id+"&u_id2="+u_id2,function(obj,error){
            console.log(obj);
            checkAccount.u_id = null;
            checkAccount.active = false;

            self.tip(obj.info);
            if(obj.code == 2){
                self.initUserInfo();
            }
        });
    },
    // 月份数据填充
    initDays:function(date2msg,month1){
        var date = new Date(this.year,this.month,0);
        var days = date.getDate();
        var firstDay = new Date(this.year,this.month - 1,1).getDay();
        var lastDay = new Date(this.year,this.month - 1,days).getDay();

        console.log("date:" + date + ",days:" + days + ",zhou:" + firstDay);
        // var month1 = this.qiandaoPage.getChildByName("body").getChildByName("month1");

        month1.removeAllChildren();
        // 填补前面
        for (var i = 0; i < firstDay; i++) {
            var tempday = cc.instantiate(this.dayPre);
            tempday.getChildByName("Label").getComponent(cc.Label).string = "";

            tempday.parent = month1;
        }
        // console.log(this.date2msg);

        var self = this;
        for (var i = 1; i <= days; i++) {
            var tempday = cc.instantiate(this.dayPre);
            tempday.getChildByName("Label").getComponent(cc.Label).string = i + "";
            tempday.dayId = i;
            tempday.parent = month1;
            if(!isnullOrundefined(date2msg[i])){
                console.log(i);
                tempday.color = cc.Color.GREEN;
                tempday.msgInfo = date2msg[i];
            }
            tempday.on("touchstart",function(){
                self.checkQiandao(this);
            });
        }
        // 填补后面

        for (var i = 6; i > lastDay; i--) {
            var tempday = cc.instantiate(this.dayPre);

            tempday.getChildByName("Label").getComponent(cc.Label).string = "";

            tempday.parent = month1;
        }

    },
    // 获取签到 详细信息
    checkQiandao:function(event){
        console.log("checkQiandao");
        if(isnullOrundefined(event.msgInfo)){
            return;
        }

        this.qiandaoInfo.active = true;
        this.qiandaoPage.active = false;
        this.myInfoPage.active = false;

        // 初始化类型
        var self = this;
        var uid;
        if(!isnullOrundefined(this.myInfoPage.getChildByName("qiandaoPage").friend_uid)){
            uid = this.myInfoPage.getChildByName("qiandaoPage").friend_uid;
        }else{
            uid = cc.sys.localStorage.getItem('uid');
        }
        
        var date = this.year + "-" + (this.month > 9 ? this.month : "0"+this.month) + "-" + event.dayId;
        jqueryHttp(url + "getMsgAndInfo?u_id="+uid+"&date="+date,function(obj,error){
            console.log(obj);
            if(isnullOrundefined(obj)){
               return; 
            }

            var typecontent = self.qiandaoInfo.getChildByName("body").getChildByName("type").getChildByName("view").getChildByName("content");
            typecontent.removeAllChildren();
        
            // 初始化 所有消息
            var content = self.qiandaoInfo.getChildByName("body").getChildByName("showPage").getChildByName("view").getChildByName("content");
            content.removeAllChildren();
            for (var i = 0; i < obj.length; i++) {
                var msg = obj[i];
                var node = cc.instantiate(self.msgPre);

                if(msg.ctime == 0){
                    node.getChildByName("time").getComponent(cc.Label).string = "00:00:00";
                }else{
                    node.getChildByName("time").getComponent(cc.Label).string = getDataByType(msg.ctime,null);
                }
                
                if(isnullOrundefined(msg.name)){
                    node.getChildByName("type").getComponent(cc.Label).string = "无";
                }else{
                    node.getChildByName("type").getComponent(cc.Label).string = msg.name;
                }
                

                node.getChildByName("msg").getComponent(cc.Label).string = msg.info;
                node.parent = content;
            }

            var tip = self.qiandaoInfo.getChildByName("body").getChildByName("tip");
            tip.getChildByName("date").getComponent(cc.Label).string = self.year + "-" + self.month + "-" + event.dayId;
            tip.getChildByName("number").getComponent(cc.Label).string = obj.length + " 次";
        });

       
    },

    initUserInfo:function(){
        // console.log("on load ");
        var self = this;
        var uid = cc.sys.localStorage.getItem('uid');
        if(isnullOrundefined("uid")){
            this.tip("登录失败 请重新登录");
            cc.director.loadScene('login');
            return;
        }
        jqueryHttp(url + "getUserInfo?andfriend=1&u_id="+uid,function(obj,error){
            console.log(obj);
            

            self.myInfoPage.getChildByName("body").getChildByName("info").getChildByName("name").getComponent(cc.Label).string = obj.user.name;
            var friendList = self.myInfoPage.getChildByName("body").getChildByName("friend").getChildByName("friendList");
            
            var content = friendList.getChildByName("view").getChildByName("content")
            content.removeAllChildren();
            if(isnullOrundefined(obj.Friend)) return;

            for (var i = 0; i < obj.Friend.length; i++) {
                var f = obj.Friend[i];
                var it = cc.instantiate(self.friPre);

                it.getComponent(cc.Label).string = f.name;
                it.u_id = f.id;
                it.on("touchstart",function(event){
                    // console.log(event.target);
                    this.initFriendMsgInfo(event.target.u_id);
                }.bind(self));

                it.parent = content;

                self.initSignInfo();
            }
        });
    },
    initSignInfo:function(uid){

        if(isnullOrundefined(uid)) uid = cc.sys.localStorage.getItem('uid');

        var self = this;
        var date = this.year + "-" + (this.month > 9 ? this.month : "0"+this.month);
        jqueryHttp(url + "getUserSign?u_id="+uid+"&date="+date,function(obj,error){
            console.log(obj);   
            if(isnullOrundefined(obj.info)){
                return;
            }
            var date2msg = {};
            // 整理数据 day => msg
            for (var i = 0; i < obj.info.length; i++) {
                // obj.info[i]

                var msg = obj.info[i];
                if(isnullOrundefined(date2msg[msg.day])){
                    date2msg[msg.day] = new Array();
                }
                date2msg[msg.day].push(msg);
            }
            self.date2msg = date2msg;
            self.initDays(date2msg,self.qiandaoPage.getChildByName("body").getChildByName("month1"));
        });
    },
    showTypeContent:function(){
        var type = this.qiandaoPage.getChildByName("type");
        if(type.active == true){
            type.active = false;
        }else{
            type.active = true;
        }
    },
    signOk:function(){
        // 签到
        var str = this.qiandaoPage.getChildByName("add").getChildByName("EditBoxName").getComponent(cc.EditBox).string;
        if(str == ""){
            return;
        }

        var t_id = this.qiandaoPage.getChildByName("typeok").typeID;
        if(isnullOrundefined(t_id)){
            t_id = 0;
        }
        var self = this;
        var uid = cc.sys.localStorage.getItem('uid');
        jqueryHttp(url + "insertSign?info="+str+"&t_id=" + t_id + "&u_id=" + uid,function(obj,error){
            console.log(obj);
            if(obj == 1){
                self.tip("签到成功");
                self.initSignInfo();
            }else{
                self.tip("签到失败");
            }
            self.qiandaoPage.getChildByName("add").getChildByName("EditBoxName").getComponent(cc.EditBox).string = "";
        });
    },

    changeDayInfo:function(event,type){
        if(this.changeMonth(type)){
            if(this.myInfoPage.getChildByName("qiandaoPage").active){  // 好友签到
                this.initFriendMsgInfo();
            }else{
                this.initSignInfo();    
            }
        }
    },

    changeMonth:function(type){
        if(type == "next"){
            if(this.changeNumber + 1 > 0){ // 下个月就别查了
                return false;
            }

            this.changeNumber++;
            if(this.month == 12){
                this.month = 1;
                this.year++;
        
            }else{
                this.month++;    
            }
            
        }else if (type == "last"){
            this.changeNumber--;
            if(this.month == 1){
                this.month = 12;
                this.year--;
                // return;
            }else{
                this.month--;    
            }
        }

        // console.log("year")
        this.qiandaoPage.getChildByName("body").getChildByName("New Layout").getChildByName("date").getComponent(cc.Label).string = this.year+"-"+this.month;
        this.myInfoPage.getChildByName("qiandaoPage").getChildByName("body").getChildByName("New Layout").getChildByName("date").getComponent(cc.Label).string = this.year+"-"+this.month;
        return true;
    },
    closeQianDaoInfo:function(){
        this.qiandaoInfo.active = false;
        if(!isnullOrundefined(this.myInfoPage.getChildByName("qiandaoPage").friend_uid)){
            this.myInfoPage.active = true;
        }else{
            this.qiandaoPage.active = true;    
        }
    },

    // 查看朋友的签到
    initFriendMsgInfo:function(u_id){
        // var friend = event.target;
        var friSignPage = this.myInfoPage.getChildByName("qiandaoPage");
        // console.log("initFriendMsgInfo:" + u_id);
        if(u_id==cc.sys.localStorage.getItem("uid")) return;
        if(isnullOrundefined(u_id)) u_id = friSignPage.friend_uid;

        friSignPage.friend_uid = u_id;
        friSignPage.active = true;
        // this.myInfoPage.getChildByName("qiandaoPage")

        var self = this;
        var date = this.year + "-" + (this.month > 9 ? this.month : "0"+this.month);
        jqueryHttp(url + "getUserSign?u_id="+u_id+"&date="+date,function(obj,error){
            console.log(obj);   
            if(isnullOrundefined(obj.info)){
                return;
            }
            var date2msg = {};
            // 整理数据 day => msg
            for (var i = 0; i < obj.info.length; i++) {
                // obj.info[i]

                var msg = obj.info[i];
                if(isnullOrundefined(date2msg[msg.day])){
                    date2msg[msg.day] = new Array();
                }
                date2msg[msg.day].push(msg);
            }
            friSignPage.getChildByName("name").getChildByName("name").getComponent(cc.Label).string = obj.user.name;

            // self.date2msg = date2msg;
            self.initDays(date2msg,friSignPage.getChildByName("body").getChildByName("month1"));
        });
    },
    closeFriendMsgInfo:function(){
        this.myInfoPage.getChildByName("qiandaoPage").friend_uid = null;
        this.myInfoPage.getChildByName("qiandaoPage").active = false;
    },

    // 提示
    tip:function ($msg){
        var tip = this.node.getChildByName("tip");
        tip.getChildByName("msg").getComponent(cc.Label).string = $msg;
        tip.active = true;
        setTimeout(function(){tip.active=false;},3000);
    },
});
